#!/usr/bin/python3

import ModTestParameters
import SMART
import sys
import csv
import os.path

# Required
M = int(sys.argv[1])
TASK_TARGET = int(sys.argv[2])
FILE_OUT = sys.argv[3]
FILE_OUT_SUMMARY = FILE_OUT + "-summary"

if os.path.isfile(FILE_OUT):
    print("Output file " + FILE_OUT + " already exists. Aborting...")
    exit(1)

# Optional multipliers
ENABLE_OPTIMAL = bool(int(sys.argv[4])) if len(sys.argv) > 5 else False 
STRENGTH_STDEV_MULTIPLIER = int(sys.argv[5]) if len(sys.argv) >= 7 else 1
FRIEND_STDEV_MULTIPLIER = int(sys.argv[6]) if len(sys.argv) >= 7 else 1

# These parameters are derived from a statistical analysis of experimental results
FRIEND_MEAN = STRENGTH_MEAN = 0.715825523745
FRIEND_STDEV = 0.0426836366557
STRENGTH_STDEV = 0.130921998163

# These are fixed parameters chosen subjectively
STEP = .05
utilRanges=[(0, 1), (0, .4), (.3, .7), (.6, 1)] # Wide, low, mid, high. TODO: Bimodel

# DISSERTATION might be a better way to format output

# print headers to FILE_OUT_SUMMARY

setsCompleted=1
for utilBound in utilRanges:
    for strength_stdev in [STRENGTH_STDEV * x for x in [STRENGTH_STDEV_MULTIPLIER]]:
        for friend_stdev in [FRIEND_STDEV * x for x in [FRIEND_STDEV_MULTIPLIER]]:
            # DISSERTATION
            # param results gives theBins; see explanation in SMART.py
            paramResults = ModTestParameters.runParamTest(ENABLE_OPTIMAL, M, STEP, TASK_TARGET, .5 * M, utilBound[0], utilBound[1], 10, 100, strength_stdev, friend_stdev, STRENGTH_MEAN, FRIEND_MEAN)
            # DISSERTATION
            # is there a better way to output results?
            with open(FILE_OUT, "a") as f:
                print("*****", file=f) # Sample delimiter
                csvWriter = csv.writer(f)
                csvWriter.writerow([M, STEP, TASK_TARGET, utilBound[0], utilBound[1], 10, 100, strength_stdev, friend_stdev])
                binSize = M
                # DISSERTATION
                # "row" is one entry in theBins
                # first row after header gives the number of task systems that were attempted for this scenario,
                # e.g. this graph.
                # Each additonal row will be[systemsSchedViaMethod1, systemsSchedViaMethod2...systemsSchedViaLastMethod]
                # More thoughts:
                # This format sucks, but if I keep it this way I can run the graph-building code with minimal changes.
                # Keep what's here, but write something else that will output all results in a single, sortable
                # spreadsheet with one line per graph.
                # The spreadsheet should include some form of area under curve.
                # area under curve needs to be included in each graph header.
                for row in paramResults:
                    csvWriter.writerow([binSize] + row)
                    binSize += STEP
            # User-visible status
            print(FILE_OUT, setsCompleted, utilBound[0], utilBound[1], strength_stdev, friend_stdev)
            setsCompleted += 1
            # with open (FILE_OUT_SUMMARY, "a) as f:
            # all of the following need to go on one row
            # print the parameters that let us identify the scenario
            # for each method, print area under curve

print(FILE_OUT, "Complete!")
